/**
 *
 */
package org.tgcloud.group.core;

import sun.util.calendar.CalendarUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author ycf 2016年3月3日
 *
 */
public class DateUtil {

    /**
     * 时间戳转换成时间字符串
     * */
    public static String changeDateFromLong(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(time * 1000));
    }

    public static String getStrDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String getStrDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String changeStrDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String getyyyyMMddHH() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        return sdf.format(new Date());
    }

    public static String getyyyy_MM_dd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    public static String getyyyy_MM_dd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getyyyyMMddSlash(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(date);
    }

    public static String getyyyyMMddToRedis(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
        return sdf.format(date);
    }

    public static String getyyyyMMdd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    public static String getyyyyMMddHHmmss() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }

    public static String getyyyyMMddHHmm() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        return sdf.format(new Date());
    }

    public static String getyyyyMMddHHmmss(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(date);
    }

    public static String getyyyy_MM_ddHHmm(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    public static String getTimeMsg(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 两个Double数相除，并保留scale位小数
     * @param v1
     * @param v2
     * @param scale
     * @return Double
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /**
     * 完善Map ,将map中的值组装成List<Map<String,Object>>
     * map 需要完善的map
     * bigName 分装的Map名字
     * smallName Map中具体分装的小的名字
     * */
    public static List<Map<String, Object>> perfectMap(String info, String[] smallName) {
        if (null == info || smallName.length == 0) {
            return null;
        }
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] smallNumber = String.valueOf(info).trim().split(",");
        for (String string : smallNumber) {
            Map<String, Object> tempMap = new HashMap<String, Object>();
            String temp[] = string.trim().split("\\|");
            for (int i = 0; i < temp.length; i++) {
                tempMap.put(smallName[i], temp[i]);
            }
            list.add(tempMap);
        }
        return list;
    }

    /**
     * @Author SGY
     * @Date 2019/7/17 14:02
     * @Param
     * @return
     * @Description // 获取第二天0点的毫秒数
     **/
    public static Long getNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * @Author SGY
     * @Date 2019/7/17 14:02
     * @Param
     * @return
     * @Description // 格式化时间：03-08(周四) 12:00-13:00
     **/
    public static String formatterDate(long start, long end) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        String date = sdf.format(new Date(start));
        SimpleDateFormat sh = new SimpleDateFormat("HH:mm");
        String shour = sh.format(new Date(start));
        SimpleDateFormat eh = new SimpleDateFormat("HH:mm");
        String ehour = eh.format(new Date(end));
        String format = date + " (" + getWhatDay(start) + ") " + shour + "-" + ehour;
        return format;
    }

    /**
     * 将时，分，秒，以及毫秒值设置为0
     *
     * @param calendar
     */
    private static void zeroFromHour(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }


    /**
     * 将时间戳转换成当天零点的时间戳
     *
     * @param milliseconds
     * @return
     */
    private static Calendar zeroFromHour(long milliseconds) {
        Calendar calendar = Calendar.getInstance(); // 获得一个日历
        calendar.setTimeInMillis(milliseconds);
        zeroFromHour(calendar);
        return calendar;
    }

    /**
     * 最终调用方法
     * @param timeStamp
     * @return
     */

    public static String getWhatDay(long timeStamp) {
        Calendar cal = zeroFromHour(timeStamp);
        String whatDay = "";
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            whatDay = "周六";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            whatDay = "周日";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            whatDay = "周一";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            whatDay = "周二";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
            whatDay = "周三";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            whatDay = "周四";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            whatDay = "周五";
        }
        return whatDay;
    }

    public static String getYesterDay_yyyyMMdd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(1577859629000l)); // 把当前时间赋给日历
        calendar.add(calendar.DATE, -1); // 设置为前一天
        Date time = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(time);
    }

    public static void main(String[] args) {
        String s = getYesterDay_yyyyMMdd();
    }


    /**
     * 毫秒转化为天小时分钟秒
     *
     * @param ms 毫秒值
     * @return
     */
    public static String formatTimeMS(Long ms) {
        if (ms == null) {
            return "0";
        }
        int ss = 1000;
        int mi = ss * 60;
        int HH = mi * 60;
        int dd = HH * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / HH;
        long minute = (ms - day * dd - hour * HH) / mi;
        //double second = (ms - day * dd - hour * HH - minute * mi) * 1.0 / ss;
        // long milliSecond = ms - day * dd - hour * HH - minute * mi - second * ss;

        if (day > 0) {
            return day + "天" + hour + "小时" + minute + "分钟";
        } else if (hour > 0) {
            return hour + "小时" + minute + "分钟";
        } else if (minute > 0) {
            return minute + "分钟";
        } else {
            return 0 + "分钟";
        }
    }

    public static String formatTimeMSH(Long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int HH = mi * 60;
        int dd = HH * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / HH;
        long minute = (ms - day * dd - hour * HH) / mi;
        //double second = (ms - day * dd - hour * HH - minute * mi) * 1.0 / ss;
        // long milliSecond = ms - day * dd - hour * HH - minute * mi - second * ss;

        if (day > 0) {
            return day + "d" + hour + "h" + minute + "m";
        } else if (hour > 0) {
            return hour + "h" + minute + "m";
        } else if (minute > 0) {
            return minute + "m";
        } else {
            return 0 + "m";
        }
    }

    /**
     * @author: zhy
     * @description: 计算i个月前
     * @create: 2020年10月19日 17:44
     * @return
     */
    public static Date getLastMonth(int i) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - i); // 设置为上一个月
        date = calendar.getTime();
        return date;
    }


    /**
     * @author: zhy
     * @description: 判断两个时间段是否相隔在7天内
     * @create: 2020年10月27日 10:20
     * @return
     */
    public static boolean isLatestWeek(Date addTime, Date now) {
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(now);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7);  //设置为7天前
        Date before7days = calendar.getTime();   //得到7天前的时间
        if (before7days.getTime() < addTime.getTime()) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean isOneWeek(Date start, Date end) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(start);
        c1.set(Calendar.HOUR_OF_DAY, 0);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        c1.set(Calendar.MILLISECOND, 0);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(start);
        c2.set(Calendar.HOUR_OF_DAY, 0);
        c2.set(Calendar.MINUTE, 0);
        c2.set(Calendar.SECOND, 0);
        c2.set(Calendar.MILLISECOND, 0);
        long time = c2.getTimeInMillis() - c1.getTimeInMillis();
        if (time == 0 || time < 7 * 24 * 60 * 60 * 1000) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取前一天时间零点时间
     * @return
     */
    public static Date getYesterDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 把当前时间赋给日历
        calendar.add(calendar.DATE, -1); // 设置为前一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return time;
    }


    public static String getYesterDayString() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 把当前时间赋给日历
        calendar.add(calendar.DATE, -1); // 设置为前一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return getyyyyMMdd(time);
    }

    /**
     * @author: zhy
     * @description: 设置天，小时
     * @create: 2020年10月30日 14:54
     * @return
     */
    public static Long getDelTime(Integer hour, Integer second, Integer minute, Integer day) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.DATE, day == null ? 0 : day);
        calendar.add(Calendar.HOUR_OF_DAY, hour == null ? 0 : hour);
        calendar.add(Calendar.SECOND, second == null ? 0 : second);
        calendar.add(Calendar.MINUTE, minute == null ? 0 : minute);
        Date newDate = calendar.getTime();
        System.out.println(newDate.getTime());
        System.out.println(date.getTime());
        return newDate.getTime() - date.getTime();
    }

    /**
     * 获取当天零点时间戳
     */
    public static Long getTodayTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 把当前时间赋给日历
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return time.getTime();
    }

    /**
     * 获取第二天零点时间戳
     */
    public static Long getTomorrowTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 把当前时间赋给日历
        calendar.add(calendar.DATE, 1); // 设置为后一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return time.getTime();
    }

    /**
     * 获取指定日期所在月份开始的时间戳
     * @param date 指定日期
     * @return
     */
    public static Long getMonthBegin(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND, 0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTimeInMillis();
    }

    /**
     * 获取指定日期所在月份结束的时间戳
     * @param date 指定日期
     * @return
     */
    public static Long getMonthEnd(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND, 59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        return c.getTimeInMillis();
    }


    public static Date getFiveYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 把当前时间赋给日历
        calendar.add(Calendar.YEAR, -5); // 设置为前一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return time;
    }


    /**
     * @description: 获取某个时间为第几周
     * @create: 2020年11月23日 09:44
     * @return
     */
    public static int getWeeksMONDAYByTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(time);
        int weeks = calendar.get(Calendar.WEEK_OF_YEAR);
        return weeks;
    }

    /**
     * @author: zhy
     * @description: 指定日期周一时间
     * @create: 2020年11月23日 11:40
     * @return
     */
    public static Date getWeekTimeByTime(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        //将小时至00
        cal.set(Calendar.HOUR_OF_DAY, 00);
        //将分钟至00
        cal.set(Calendar.MINUTE, 00);
        //将秒至00
        cal.set(Calendar.SECOND, 00);
        //将毫秒至000
        cal.set(Calendar.MILLISECOND, 000);
        return cal.getTime();
    }

    // 获得本周日24点时间
    public static Date getTimesWeeknight(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getWeekTimeByTime(time));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        //将小时至23
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        cal.set(Calendar.MINUTE, 59);
        //将秒至59
        cal.set(Calendar.SECOND, 59);
        //将毫秒至999
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }


    // 获得当天24点时间
    public static Date getTimesDayNight(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getWeekTimeByTime(time));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        //将小时至23
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        cal.set(Calendar.MINUTE, 59);
        //将秒至59
        cal.set(Calendar.SECOND, 59);
        //将毫秒至999
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static String getyyyyMM(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.format(new Date(time));
    }

    public static Date getTimeByYearMonth(String substring) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.parse(substring);
    }

    /**
     * 获取零点时间
     * @param date
     * @return
     */
    public static Date getEarlyByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取年月
     * @param time
     * @return
     */
    public static String getyyyyMM(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.format(time);
    }

    /**
     * 获取年份
     * @param timeValue
     */
    public static String getyyyyByDate(Date timeValue) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(timeValue);
    }

    /**
     * 获取月份
     * @param timeValue
     */
    public static String getMMByDate(Date timeValue) {
        SimpleDateFormat sdf = new SimpleDateFormat("M");
        return sdf.format(timeValue);
    }

    /**
     * 获取年周
     * @param time
     * @return
     */
    public static String getyyyyWeeks(Date time) {
        String year = getyyyyByDate(time);
        int weeksMONDAYByTime = getWeeksMONDAYByTime(time);
        return year + StringUtil.complement2digit(weeksMONDAYByTime);
    }

    public static Date tansofDate(String createTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(createTime);
        return parse;
    }

    /**
     * 判断最新时间和上传时间前天的大小
     * @param newDate 数据库最新时间
     * @param uploadDate 上传时间
     * @return true-是前天或前天以前的 false-不是
     */
    public static boolean isBeforeYesterday(Date newDate, Date uploadDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(newDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long newTime = calendar.getTimeInMillis();

        calendar.setTime(uploadDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 上传时间0点
        long uploadTime = calendar.getTimeInMillis();

        return (uploadTime - newTime) >= 1 * 24 * 60 * 60 * 1000l;
    }


    /**
     * @author: zhy
     * @description: 7天前的时间
     * @create: 2021年08月13日 14:20
     * @return
     */
    public static Date sevenDayBefore(Date date) {
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7);  //设置为7天前
        Date before7days = calendar.getTime();   //得到7天前的时间
        return before7days;
    }

    /**
     * 判断两个时间是否在同一年同一月同一日
     * @return
     */
    public static boolean equalsYearMonthDay(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        int y1 = calendar1.get(Calendar.YEAR);
        int m1 = calendar1.get(Calendar.MONTH);
        int d1 = calendar1.get(Calendar.DAY_OF_MONTH);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        int y2 = calendar2.get(Calendar.YEAR);
        int m2 = calendar2.get(Calendar.MONTH);
        int d2 = calendar2.get(Calendar.DAY_OF_MONTH);
        return y1 == y2 && m1 == m2 && d1 == d2;
    }


    /**
     * @author: zhy
     * @description: 获取30分钟前的时间
     * @create: 2021年12月07日 09:38
     * @return
     */
    public static Date getLastMinute() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.add(Calendar.MINUTE, -30);
        date = calendar.getTime();
        return date;
    }


    /**
     * @author: zhy
     * @description:转不同时区
     * @create: 2021年12月08日 11:24
     * isDST：true为夏令时
     * zone: 中国时区GMT+08
     * @return
     */
    public static Date convertTimezone(Date time, String zone, boolean isDST){
        String[] split = zone.split("\\+");
        String num = null;
        boolean isAdd = false;
        boolean isDel = false;
        if (split.length > 1){
            isAdd = true;
            num = split[1];
        }else {
            String[] delSplit = zone.split("\\-");
            if (delSplit.length > 1){
                isDel = true;
                num = delSplit[1];
            }
        }
        long time1 = time.getTime();
        if (isAdd){
            time1 = time1 + Long.valueOf(num) * 60 * 60 * 1000;
        }
        if (isDel){
            time1 = time1 - Long.valueOf(num) * 60 * 60 * 1000;
        }
        if (isDST){
            time1 = time1 + 1 * 60 * 60 * 1000;
        }
        TimeZone timeZone = TimeZone.getTimeZone(zone);
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeZone(timeZone);
        calendar.setTimeInMillis(time1);
        return calendar.getTime();
    }


    public static Date getTomoral(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 把当前时间赋给日历
        calendar.add(calendar.DATE, 1); // 设置为后一天
        Date time = calendar.getTime();
        return time;
    }

    public static Date dateToISODate(Date dateStr) {
        Date parse = null;
        try {
// 解析字符串时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            parse = format.parse(format.format(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }


    /**
     * @author: zhy
     * @description: 获取所选时间的季度
     * @create: 2023年12月01日 13:53
     * @return
     */
    public static String getQuarterByDate(Date timeValue) {
        SimpleDateFormat sdf = new SimpleDateFormat("M");
        String format = sdf.format(timeValue);
        String quarter = "";
        if (format.equals("1") || format.equals("2") || format.equals("3")){
            quarter = "1";
        }else if (format.equals("4") || format.equals("5") || format.equals("6")){
            quarter = "2";
        }else if (format.equals("7") || format.equals("8") || format.equals("9")){
            quarter = "3";
        }else if (format.equals("10") || format.equals("11") || format.equals("12")){
            quarter = "4";
        }
        return quarter;
    }

    /**
     * @author: zhy
     * @description: 根据季度获取时间
     * @create: 2023年12月01日 13:57
     * @return
     */
    public static Map<String, Date> getQuarterStartTimeAndEndTimeByDate(Date timeValue) {
        Map<String, Date> map = new HashMap<String, Date>();
        Date startTime = new Date();
        Date endTime = new Date();
        try {
            String quarter = getQuarterByDate(timeValue);
            String year = DateUtil.getyyyyByDate(timeValue);
            if (quarter.equals("1")){
                startTime = DateUtil.tansofDate(year + "-01-01");
                endTime = DateUtil.tansofDate(year + "-03-31");
            }else if (quarter.equals("2")){
                startTime = DateUtil.tansofDate(year + "-04-01");
                endTime = DateUtil.tansofDate(year + "-06-30");
            }else if (quarter.equals("3")){
                startTime = DateUtil.tansofDate(year + "-07-01");
                endTime = DateUtil.tansofDate(year + "-09-30");
            }else if (quarter.equals("4")){
                startTime = DateUtil.tansofDate(year + "-10-01");
                endTime = DateUtil.tansofDate(year + "-12-31");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return map;
    }


    /**
     * @author: zhy
     * @description: 获取几周或几月前的时间
     * @create: 2023年12月11日 13:39
     * @return
     */
    public static Date getChooseEndDate(Date date, int num, int numType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 把当前时间赋给日历
        if (numType == 1){
            Integer i = num * 7 - 1;
            if (num < 0){
                i = num * 7 + 1;
            }
            calendar.add(calendar.DATE, i); // 设置为前几周
        }else {
            calendar.add(calendar.MONTH, num); // 设置为前几月
        }
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        calendar.set(Calendar.MILLISECOND, 000);
        Date time = calendar.getTime();
        return time;
    }

    public static Date getEndDate(Date date, int num, int numType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 把当前时间赋给日历
        if (numType == 1){
            calendar.add(calendar.DATE, num); // 设置为天
        }else {
            calendar.add(calendar.MONTH, num); // 设置为前几月
        }
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        calendar.set(Calendar.MILLISECOND, 000);
        Date time = calendar.getTime();
        return time;
    }

    public static Date getChooseDate(Date date, int dayType, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 把当前时间赋给日历
        if (dayType == 1){
            calendar.add(calendar.DATE, num); // 设置为前几天
        }else {
            calendar.add(calendar.MONTH, num); // 设置为前几月
        }
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        calendar.set(Calendar.MILLISECOND, 000);
        Date time = calendar.getTime();
        return time;
    }


    /**
     * @author: zhy
     * @description: 获取时间段内的每一天
     * @create: 2023年12月11日 14:35
     * @return
     */
    public static List<Date> getDateListByStartTimeAndEndTime(Date startTime, Date endTime) {
        List<Date> dateList = new ArrayList<>();
        Date currentDate = startTime;
        while (!currentDate.after(endTime)){
            dateList.add(currentDate);
            currentDate = DateUtil.getTomoral(currentDate);
        }
        return dateList;
    }


    // 获取时间段内的每一个月
    public static List<YearMonth> getMonthListByStartTimeAndEndTime(Date startTime, Date endTime) {
        List<YearMonth> months = new ArrayList<>();
        YearMonth startYM = YearMonth.from(startTime.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate());
        YearMonth endYM = YearMonth.from(endTime.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate());
        while (startYM.isBefore(endYM)) {
            months.add(startYM);
            startYM = startYM.plusMonths(1);
        }
        // Include the end month
        months.add(endYM);
        return months;
    }

    // 将yearmonth转换为date
    public static Date getDateStringByYearMonth(YearMonth yearMonth) {
        return Date.from(yearMonth.atEndOfMonth().atStartOfDay(ZoneId.systemDefault()).toInstant());
    }


    public static Date getEndHourByDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 把当前时间赋给日历
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date time = calendar.getTime();
        return time;
    }



    public static Integer getyyyyMMddToInteger(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(date);
        return Integer.valueOf(format);
    }



    // 获取时间段内的每一个月
    public static List<Date> getMonthDateListByStartTimeAndEndTime(Date startTime, Date endTime) {
        List<YearMonth> months = getMonthListByStartTimeAndEndTime(startTime, endTime);
        List<Date> dateList = new ArrayList<>();
        for (YearMonth yearMonth : months) {
            // 将 YearMonth 转换为 LocalDate，这里选择每月的第一天
            LocalDate localDate = yearMonth.atDay(1);
            // 将 LocalDate 转换为 Date
            Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            dateList.add(date);
        }
        return dateList;
    }

}
